<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="spanishgringo.data.GoogleData" %>
<%@ page import="java.util.Locale" %>
<%
  GoogleData googleData = (GoogleData)request.getAttribute("googleData");
%>

    <%
       if (googleData.getDataListError() != null) {
         out.println("<div class='queryError'>" + googleData.getDataListError() + " </div>");
       }
       else {
         // Create an HTML table of the Google Analytics profile data.
         if (googleData.getDataList().size() == 0) {
           out.println("No Data Found");
         } else {
           //Create the Table Head with the Country Name, dates and segment Selected
        	StringBuffer tableHead = new StringBuffer("<h3 class='siteName ui-state-default'><label><input type='checkbox' checked />");
           tableHead.append(googleData.getProfileName());

          	tableHead.append("</label><span class='dataParams'>");
          	//insert date and segment here
          	tableHead.append("Segment: " + googleData.getSegmentName()+" | Start Date: "+ googleData.getStartDate() +" | End Date: "+ googleData.getEndDate());
          	//tableHead.append("<small class='dataURL'>"+ googleData.getQueryURL() +"</small>");
          	tableHead.append("</span>");
           	tableHead.append("<span class='ui-icon ui-icon-arrowthick-2-n-s'></span></h3>\n");
           	
           Boolean first = true;
           StringBuffer table = new StringBuffer("<table>");
           for (String[] gaData : googleData.getDataList()) {
             table.append("<tr>");
             for (int i = 0; i < gaData.length; i++) {
               if (first) {
                 table.append(String.format("<th><a class='colHead' href='#'>%s</a></th>", gaData[i]));
                
               } else {
            	   if(i<2 || i==gaData.length-1){
                 		table.append(String.format("<td>%s</td>", gaData[i]));
            	   }else{
            		   if(Math.floor(Double.valueOf(gaData[i]))==Double.valueOf(gaData[i])&& gaData[i].matches("^[0-9]+$")){
            		   		table.append(String.format(Locale.US,"<td>%,d</td>", Long.valueOf(gaData[i])));
            		   }else{
            			   table.append(String.format(Locale.US,"<td>%.2f%%</td>", Double.valueOf(gaData[i])));
	            		   }
            	   }
               }
             }
             table.append("</tr>\n");
             first = false;
           }
           table.append("</table>");
           
           out.println(tableHead.toString());
           out.println(table.toString());
         }
       }
    %>


  